package com.web.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.web.entity.NoteType;
import com.web.entity.Token;
import com.web.mapper.NoteTypeMapper;
import com.web.service.INoteTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.web.service.ITokenService;
import com.web.util.LoginException;
import com.web.util.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 笔记类型表 服务实现类
 * </p>
 *
 * @author hgjxyz
 * @since 2020-11-18
 */
@Service
public class NoteTypeServiceImpl extends ServiceImpl<NoteTypeMapper, NoteType> implements INoteTypeService {

    @Autowired private ITokenService tokenService;

    @Override
    public NoteType add(String name, String tokenStr) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(tokenStr);
        Wrapper<NoteType> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", token.getId());
        if(this.selectCount(wrapper) > 200) throw new ServiceException("单个用户最多只能有200个笔记类型");
        NoteType noteType = new NoteType();
        noteType.setId(UUID.randomUUID().toString());
        noteType.setUserId(token.getId());
        noteType.setTitle(name);
        noteType.setIsDelete(0);
        noteType.setCreatedTime(new Date());
        if(!this.insert(noteType))throw new ServiceException("新增失败");
        return noteType;
    }

    @Override
    public void delete(String id, String tokenStr) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(tokenStr);
        NoteType noteType = this.selectById(id);
        if(noteType == null || noteType.getIsDelete() == 1 || !noteType.getUserId().equals(token.getId()))
            throw new ServiceException("未找到这条记录");
        noteType.setIsDelete(1);
        if(!this.updateById(noteType)) throw new ServiceException("删除失败");
    }

    @Override
    public void update(String id, String name, String tokenStr) throws ServiceException, LoginException {
        Token token = this.tokenService.getByToken(tokenStr);
        NoteType noteType = this.selectById(id);
        if(noteType == null || noteType.getIsDelete() == 1 || !noteType.getUserId().equals(token.getId()))
            throw new ServiceException("未找到这条记录");
        noteType.setTitle(name);
        if(!this.updateById(noteType)) throw new ServiceException("修改失败");
    }

    @Override
    public List<NoteType> getList(String tokenStr)throws LoginException {
        Token token = this.tokenService.getByToken(tokenStr);
        Wrapper<NoteType> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", token.getId())
                .eq("is_delete", 0)
                .orderBy("created_time", false);
        return this.selectList(wrapper);
    }

    @Override
    public NoteType get(String id) {
        NoteType type = this.selectById(id);
        if(type != null && type.getIsDelete() == 1){
            return null;
        }
        return type;
    }
}
